package com.jiamao.goplay.adapter;

import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.RequiresApi;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jiamao.goplay.R;
import com.jiamao.goplay.ui.discover.vm.CityItemVM;
import com.jiamao.goplay.ui.discover.vm.CityMenuItemVM;
import com.netease.nim.uikit.common.util.sys.ScreenUtil;

import me.goldze.mvvmhabit.binding.viewadapter.recyclerview.LayoutManagers;
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;

public class CityTextListAdapter extends BindingRecyclerViewAdapter {

    Context context;
    public CityTextListAdapter(Context context){
        this.context = context;
    }

    @Override
    public ViewDataBinding onCreateBinding(LayoutInflater inflater, @LayoutRes int layoutId, ViewGroup viewGroup) {
        ViewDataBinding binding = super.onCreateBinding(inflater, layoutId, viewGroup);
        Log.d("HTML", "created binding: " + binding);


        return binding;
    }



    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindBinding(ViewDataBinding binding, int bindingVariable, @LayoutRes int layoutId, int position, Object item) {
        super.onBindBinding(binding, bindingVariable, layoutId, position, item);

               CityItemVM vm  = (CityItemVM) item;
        RecyclerView textView  = binding.getRoot().findViewById(R.id.recyclerview);
                 TextView moreTv = binding.getRoot().findViewById(R.id.tv_more);
                if (position == 0){
                    setListViewHeight(textView);
                    moreTv.setVisibility(View.GONE);
                }else {

                    moreTv.setVisibility(View.VISIBLE);
                }

        if (position ==0 && !vm.title.get().equals("热门城市"))
        {
            textView.setLayoutManager(new GridLayoutManager(textView.getContext(), 2, RecyclerView.VERTICAL, false));
        }else {
            textView.setLayoutManager(new GridLayoutManager(textView.getContext(), 3, RecyclerView.VERTICAL, false));
        }

                if (position > 0 && vm.observableList.size() >=6){

                  //  textView.setLayoutManager(new GridLayoutManager(textView.getContext(), 2, RecyclerView.VERTICAL, false));
                    if(!vm.select.get()){
                        moreTv.setText("查看更多");
                        setListViewHeight(textView,2);
                    }else {
                        moreTv.setText("折叠");
                        setListViewHeight(textView);
                    }
                    moreTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                               //设置状态 刷新当前局部
                            vm.select.set( !vm.select.get());
                            notifyItemChanged(position);
                        }
                    });

                }else {
                    setListViewHeight(textView);
                    moreTv.setVisibility(View.GONE);
                }


    }


    void setListViewHeight(RecyclerView listview) {

      //  int totalHeight = 0;
        //listAdapter.getCount()返回数据项的数目
        int len = listview.getAdapter().getItemCount();

        int totalHeight = ((len/3)+(len%3==0?0:1) )* ScreenUtil.dip2px(46);
        Log.d("HTML","AAA"+((len%3)+(len/3==0?0:1) )+"  LEN:"+len+" totalHeight"+totalHeight);
        ViewGroup.LayoutParams  params = listview.getLayoutParams();
        params.height = totalHeight;
        listview.setLayoutParams(params);
    }

    void setListViewHeight(RecyclerView listview,int max) {

        //  int totalHeight = 0;
        //listAdapter.getCount()返回数据项的数目
        int len = listview.getAdapter().getItemCount();
        int totalHeight = max * ScreenUtil.dip2px(46);
        ViewGroup.LayoutParams  params = listview.getLayoutParams();
        params.height = totalHeight;
        listview.setLayoutParams(params);
    }

//    @Override
//    public void onBindBinding(@NonNull ViewDataBinding binding, int variableId, int layoutRes, int position, Object item) {
//        super.onBindBinding(binding, variableId, layoutRes, position, item);
//
//        if (item instanceof CityMenuItemVM){
//            CityMenuItemVM vm  = (CityMenuItemVM) item;
//
//        }
//
//    }
}
